<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';

require('echarts/theme/macarons'); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '150px',
    },
    chartData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    console.log();
    this.$nextTick(() => {
      setTimeout(() => {
        this.initChart();
      }, 100);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  methods: {
    initChart() {
      let that = this;
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      this.chart.setOption({
        color: ['#3FC529', '#e3e3e3', '#E74629', '#E2E7EE'],
        tooltip: {
          show: true,
          trigger: 'item',
          backgroundColor: '#fff',
          formatter: '{b} :{d}%',
        },
        legend: {
          show: false,
          left: 'center',
          bottom: '10',
          // data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        },

        series: [
          {
            name: '班次',
            type: 'pie',
            clockwise: true,
            radius: ['45%', '60%'],
            avoidLabelOverlap: false,
            label: {
              //标签
              normal: {
                show: false,
                position: 'outer',
                // formatter: '{d}%, {c} \n\n',
                // 模板变量有 {a}, {b}，{c}，{d}，{e}，分别表示系列名，数据名，数据值等。
                formatter: '',
                borderWidth: 20,
                borderRadius: 4,
                padding: [90, -30],
                rich: {
                  a_set: {
                    color: '#fff',
                    lineHeight: 20,
                    align: 'center',
                  },
                  c_set: {
                    color: '#fff',
                  },
                },
              },
            },
            labelLine: {
              normal: {
                show: false, //引线隐藏
                length: 20,
                length2: 40,
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },

            data: chartData,
          },
        ],
      });
    },
  },
};
</script>
